﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sherwood.SignOn.Server.Models.Repositories;
using Sherwood.SignOn.Server.Models.Repositories.Config;

namespace Sherwood.SignOn.Server.Tests
{
    /// <summary>
    /// This class contains tests for the user profile repository.
    /// The role repository should:
    ///  - automatically user IRoleProviders from assemblies specified in web.config
    ///  - return profile xml with roles / attributes from all role providers for signed on users
    ///  - return an empty string for non-existing sessions
    ///  - client should be optional
    /// </summary>
    [TestClass]
    public class UserProfileRepositoryTests : TestBase
    {
        [TestMethod]
        public void Returns_Empty_String_For_Not_Valid_Session()
        {
            //Assemble
            ConfigUserProfileRepository repository = new ConfigUserProfileRepository();

            //Act
            string profileXML = repository.GetBasicProfileXml(null, "clientA");

            //Assert
            Assert.AreEqual<string>("",profileXML,"Profile XML should be an empty string.");
        }


        [TestMethod]
        public void Ensure_Client_Is_Optional_Test()
        {
            //Assemble
            ConfigUserProfileRepository repository = new ConfigUserProfileRepository();
            IUserSession session = TestHelper.CreateDummySession();

            //Act (pass in null client, but valid session)
            string profileXML = repository.GetSessionProfileXml(session, null);

            //Assert
            Assert.IsTrue(!string.IsNullOrEmpty(profileXML));
        }

        /// <summary>
        /// Test whether roles / attributes could be reteived from mulitple clients
        /// </summary>
        [TestMethod]
        public void Ensure_Profile_Contains_Roles_And_Attributes_Test()
        {
            //Assemble
            ConfigUserProfileRepository repository = new ConfigUserProfileRepository();
            IUserSession session = TestHelper.CreateDummySession();

            //Act (pass in null client, but valid session)
            string profileXML = repository.GetSessionProfileXml(session, null);


            //Assert
            Assert.IsTrue(!string.IsNullOrEmpty(profileXML));
            getUserProfileResponse profile = getUserProfileResponse.Parse(profileXML);
            Assert.IsNotNull(profile);
            bool contiansSourceARoles = false, containsSourceBRoles = false, containsSourceAAttributes = false;
            foreach (role rl in profile.role)
            {
                if (rl.source == "SourceA")
                {
                    contiansSourceARoles = true;
                }
                if (rl.source == "SourceA")
                {
                    containsSourceBRoles = true;
                }
            }
            foreach (attribute attr in profile.attribute)
            {
                if (attr.source == "SourceA")
                {
                    containsSourceAAttributes = true;
                }
            }
            Assert.IsTrue(containsSourceAAttributes);
            Assert.IsTrue(contiansSourceARoles);
            Assert.IsTrue(containsSourceBRoles);            

        }

    }
}
